package com.viethp.mt.customactivities;

import java.util.ArrayList;

import android.app.Activity;
import android.app.ActivityGroup;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;

import com.viethp.mt.activities.AGroupHave;
import com.viethp.mt.activities.AGroupWant;
import com.viethp.mt.activities.R;

public class ATabActivityGroup extends ActivityGroup {
	public static ATabActivityGroup group;
	private static ArrayList<View> arrList, callBack;
	private int count = 0;

	private static final int REQUEST_PAYMENT = 101;
	private static final int REQUEST_DELETE = 102;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		arrList = new ArrayList<View>();
		callBack = new ArrayList<View>();
		group = this;

		View view = getLocalActivityManager().startActivity(
				"AGroupWant",
				new Intent(this, AGroupWant.class)
						.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))
				.getDecorView();

		replaceView(view);
	}

	public void replaceView(View view) {
		Animation hyperspaceJump = AnimationUtils.loadAnimation(this,
				R.anim.appear);
		view.startAnimation(hyperspaceJump);

		arrList.add(view);
		setContentView(view);
		count++;
	}

	public void back() {
		if (arrList.size() > 1) {
			// View callback = arrList.get(arrList.size() - 1);
			// callBack.add(callback);
			arrList.remove(arrList.size() - 1);
			View v = arrList.get(arrList.size() - 1);
			Animation hyperspaceJump = AnimationUtils.loadAnimation(this,
					R.anim.appear);
			v.startAnimation(hyperspaceJump);
			setContentView(v);
			count--;
		} else {
			this.finish();
		}
	}

	public void callbackView() {
		if (callBack.size() > 0) {
			// callBack.remove(callBack.size()-1);
			View callback = callBack.get(arrList.size() - 1);
			arrList.add(callback);
			callBack.remove(callBack.size() - 1);
			Animation hyperspaceJump = AnimationUtils.loadAnimation(this,
					R.anim.appear);
			callback.startAnimation(hyperspaceJump);
			setContentView(callback);
		}
	}

	@Override
	public void onBackPressed() {
		ATabActivityGroup.group.back();
	}

	public void setCount(int count) {
		this.count = count;
	}

	public int getCount() {
		return this.count;
	}

	public int getcallbackSize() {
		return callBack.size();
	}

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		switch (requestCode) {
		case REQUEST_PAYMENT:
			if (resultCode == Activity.RESULT_OK) {
				AGroupWant activity = (AGroupWant) getLocalActivityManager()
						.getCurrentActivity();
				activity.onActivityResult(requestCode, resultCode, data);
			}
			break;
		case REQUEST_DELETE:
			if (resultCode == Activity.RESULT_OK) {
				AGroupHave activity1 = (AGroupHave) getLocalActivityManager()
						.getCurrentActivity();
				activity1.onActivityResult(requestCode, resultCode, data);
			}
			break;
		default:
			break;
		}
	}
}
